<?php
namespace app\index\controller;

use app\index\model\Book;
use app\index\model\Comment;
use app\index\model\Footprint;
use think\Db;
use think\Request;
use think\Session;

class IndexController extends CommonController
{
    protected $model = null;
    //初始化操作
    protected function _initialize()
    {
        parent::_initialize();
        $request = Request::instance();
        //dump($request);
        $this->assign('request',$request);
        //echo '模型:' . $request->module() . '<br/>控制器:' .$request->controller() . '<br>方法' . $request->action();
        //halt('');
        //if ($this->model == null) $this->model = new Article();
    }
    //1.前台首页
    public function index()
    {
        //1.取出9篇新书
        $new_list = Db::name('book')->order('sort_order desc')->limit(9)->select();
        $this->assign('new_list', $new_list);

        return $this->fetch();
    }
    //2. 书籍详情页
    public function detail($id = 1)
    {
        //1.查询数据
        $data = Book::get($id);
        if (empty($data)) $this->redirect('index/index');
        $this->assign('data', $data);
        //2.记录 足迹信息 插入操作
        $user_id = Session::get('user_id');
        //2.1 查询是否已经查看过该文章
        $see = Db::name('footprint')->where('user_id', $user_id)->where('book_id',$id)->find();
        if (!empty($see)) {
            $rs = Db::name('footprint')->where('user_id', $user_id)->where('book_id',$id)->setInc('view');
        }else {
            $arr = ['user_id' => $user_id, 'book_id' => $id, 'create_time' => time(), 'update_time' => time()];
            Db::name('footprint')->insert($arr);
        }
        //3.获取评论列表内容
        $comment = Comment::where('book_id', $id)->limit(6)->select();
        $this->assign('comment', $comment);
        //4.相似书籍
        $same_list = Db::name('book')->where('category_id', $data['category_id'])->order('id desc')->limit(6)->select();
        $this->assign('same_list', $same_list);

        return $this->fetch();
    }

    //2.1 评论入库操作
    public function comment()
    {
        if (request()->isPost()) {
            $post = input('post.');

            if (!$post['book_id']) return $this->error('数据有误，稍后重试');
            //入库操作
            $post['user_id'] = Session::get('user_id');
            $post['create_time'] = time();

            if (Db::name('comment')->insert($post) )
                return $this->success('评论成功');
            else
                return $this->error('评论失败');
        }
        return $this->error('数据有误');
    }
    //2.2 阅读书籍
    public function read($id)
    {
        //1.查询数据
        $data = Book::get($id);
        if (empty($data)) $this->redirect('index/index');
        $this->assign('data', $data);
        return $this->fetch();
    }

    //3.足迹 -- 修改 为每次点击次数+1
    public function zuji()
    {
        //1.获取列表
        $list = Footprint::where('user_id', Session::get('user_id'))->order('id desc')->paginate(10);
        $this->assign('list', $list);
        return $this->fetch();
    }
    
    //4.书库列表
    public function shuku()
    {
        //取出书库列表的方法
        $list = Book::cate_book_list();
        //dump($list);exit();
        $this->assign('list', $list);
        return $this->fetch();
    }

    //4.1 具体某个书库
    public function shuku_more($id = 1)
    {
        //取出书库列表的方法
        $list = Book::cate_book_one($id);
        //dump($list);exit();
        $this->assign('list', $list);
        return $this->fetch();
    }

}
